﻿using NDF.Data.EntityFramework.Migrations;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UA.Data.Model;

namespace UA.Data
{
    public class UADemoDbMigrationConfiguration : MigrationConfiguration<UADbContext>
    {
        protected override void Seed(UADbContext context)
        {
            ClearData(context);
            InitializeData(context);
            context.SaveChanges();
        }

        public static void ClearData(UADbContext context)
        {
            context.AppDomains.RemoveRange(context.AppDomains);
            context.Applications.RemoveRange(context.Applications);
            context.AppPowers.RemoveRange(context.AppPowers);
            context.Powers.RemoveRange(context.Powers);
            context.RolePowers.RemoveRange(context.RolePowers);
            context.Roles.RemoveRange(context.Roles);
            context.Sessions.RemoveRange(context.Sessions);
            context.Tickets.RemoveRange(context.Tickets);
            context.UserRoles.RemoveRange(context.UserRoles);
            context.Users.RemoveRange(context.Users);
        }

        public static void InitializeData(UADbContext context)
        {
            // 用户帐号信息
            context.Users.Add(new User { Key = Guid.Parse("{50C13D89-B01C-4B10-B96E-E3AC3F9FEBAA}"), Code = "000", Name = "系统管理员", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1980-11-11") });
            context.Users.Add(new User { Key = Guid.Parse("{3CF893D5-4369-400F-922D-53DCBDDC55E9}"), Code = "001", Name = "张三", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1984-11-11") });
            context.Users.Add(new User { Key = Guid.Parse("{BA2428FE-97AF-443D-8B24-8ADB24764045}"), Code = "002", Name = "李四", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1984-11-11") });
            context.Users.Add(new User { Key = Guid.Parse("{340ED7A3-5DBD-43A0-B6E6-A14257DFA1DF}"), Code = "003", Name = "王五", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1984-11-11") });
            context.Users.Add(new User { Key = Guid.Parse("{0423CC64-1C60-49EF-9A94-4F9835AB6E99}"), Code = "004", Name = "赵六", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1984-11-11") });
            context.Users.Add(new User { Key = Guid.Parse("{45BC8612-47BD-407C-AB85-53F628F0CA1D}"), Code = "CJW", Name = "陈建伟", Password = "123456", Sex = Sex.Male, Birthday = Convert.ToDateTime("1989-05-11") });

            // 用户角色信息
            context.Roles.Add(new Role { Key = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), Code = "01", Name = "系统管理员" });
            context.Roles.Add(new Role { Key = Guid.Parse("{D3DB9218-731A-42F1-82AF-423DC36A4585}"), Code = "02", Name = "技术研发部经理" });
            context.Roles.Add(new Role { Key = Guid.Parse("{E1843A20-499A-418F-AC3B-5583A6B8A01C}"), Code = "03", Name = "技术研发工程师" });
            context.Roles.Add(new Role { Key = Guid.Parse("{C17D1440-D388-4940-97E0-3F6DADC93B11}"), Code = "04", Name = "普通职员" });

            // 用户帐号和用户角色的关联
            // 系统管理员 - 系统管理员
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{CA7C2CCA-E9AB-4C3A-82A0-98AF270C811D}"), UserKey = Guid.Parse("{50C13D89-B01C-4B10-B96E-E3AC3F9FEBAA}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}") });
            // 张三 - 技术研发部经理，技术研发工程师
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{6E5D2ACE-CE48-4361-B84C-0858D866F1EB}"), UserKey = Guid.Parse("{3CF893D5-4369-400F-922D-53DCBDDC55E9}"), RoleKey = Guid.Parse("{D3DB9218-731A-42F1-82AF-423DC36A4585}") });
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{8540598F-32A3-4318-96F6-807E9F664807}"), UserKey = Guid.Parse("{3CF893D5-4369-400F-922D-53DCBDDC55E9}"), RoleKey = Guid.Parse("{E1843A20-499A-418F-AC3B-5583A6B8A01C}") });
            // 李四 - 系统管理员，技术研发部经理
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{B1AD0918-4948-4CCF-BA09-70F4159BC441}"), UserKey = Guid.Parse("{BA2428FE-97AF-443D-8B24-8ADB24764045}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}") });
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{1C46F31E-56B3-4B5D-8BD7-765D7BA36432}"), UserKey = Guid.Parse("{BA2428FE-97AF-443D-8B24-8ADB24764045}"), RoleKey = Guid.Parse("{D3DB9218-731A-42F1-82AF-423DC36A4585}") });
            // 王五 - 技术研发工程师
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{7AAF728E-56F5-4A9B-AA00-779D595EE36D}"), UserKey = Guid.Parse("{340ED7A3-5DBD-43A0-B6E6-A14257DFA1DF}"), RoleKey = Guid.Parse("{E1843A20-499A-418F-AC3B-5583A6B8A01C}") });
            // 赵六 - 普通职员
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{A72B9E1D-A53C-45C5-8F49-F54F32935055}"), UserKey = Guid.Parse("{0423CC64-1C60-49EF-9A94-4F9835AB6E99}"), RoleKey = Guid.Parse("{C17D1440-D388-4940-97E0-3F6DADC93B11}") });
            // 陈建伟 - 技术研发工程师
            context.UserRoles.Add(new UserRole { Key = Guid.Parse("{8800BFFC-749D-4D63-A2A4-AA99AF500E92}"), UserKey = Guid.Parse("{45BC8612-47BD-407C-AB85-53F628F0CA1D}"), RoleKey = Guid.Parse("{E1843A20-499A-418F-AC3B-5583A6B8A01C}") });

            // 系统权限信息
            context.Powers.Add(new Power { Key = Guid.Parse("{D131C8BE-D5E5-436D-85BB-E201E7AE494E}"), Code = "01", Name = "个人信息管理", PowerType = PowerType.Menu, Url = "/Account/Manage" });
            context.Powers.Add(new Power { Key = Guid.Parse("{22AE02FA-F051-4DC5-9E8F-007243FEDA05}"), Code = "02", Name = "修改密码", PowerType = PowerType.Menu, Url = "/Account/UpdatePwd" });
            context.Powers.Add(new Power { Key = Guid.Parse("{E04107C1-D299-4915-958E-23EBCD558192}"), Code = "03", Name = "系统账户管理", PowerType = PowerType.Menu, Url = "/Users/Index" });
            context.Powers.Add(new Power { Key = Guid.Parse("{E0D29966-D221-4B0B-85DF-0E625E4E6A21}"), Code = "04", Name = "系统角色管理", PowerType = PowerType.Menu, Url = "/Roles/Index" });
            context.Powers.Add(new Power { Key = Guid.Parse("{0F0C7900-8A7C-41D1-8455-2B0990DBD2E0}"), Code = "05", Name = "外接应用系统管理", PowerType = PowerType.Menu, Url = "/Application/Index" });
            context.Powers.Add(new Power { Key = Guid.Parse("{D1CC8A3E-D91D-496E-B9B7-A467F9639A4D}"), Code = "06", Name = "用户访问状态管理", PowerType = PowerType.Menu, Url = "/Sessions/Index" });
            context.Powers.Add(new Power { Key = Guid.Parse("{0FFE3734-A059-4DD1-BA6F-74329858DB3D}"), Code = "07", Name = "数据库维护", PowerType = PowerType.Menu, Url = "/System/DbSetting" });

            // 系统权限和用户角色的关联
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{10E872FD-75B4-4CBB-ACD3-293D8F4EF5E5}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{D131C8BE-D5E5-436D-85BB-E201E7AE494E}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{281E85A6-3545-4A26-A272-0CC3DFE5E508}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{22AE02FA-F051-4DC5-9E8F-007243FEDA05}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{F3D4BE24-FA5D-4C1C-997F-1EE0AFA910EE}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{E04107C1-D299-4915-958E-23EBCD558192}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{05B3F5C1-F4CC-4A2E-B9D1-482BF044738E}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{E0D29966-D221-4B0B-85DF-0E625E4E6A21}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{8F4B4222-C1BA-44BD-AEE3-4AB10CE80E8F}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{0F0C7900-8A7C-41D1-8455-2B0990DBD2E0}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{EC7B111B-6D65-4D61-9D84-5572DCF3ACF8}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{D1CC8A3E-D91D-496E-B9B7-A467F9639A4D}") });
            context.RolePowers.Add(new RolePower { Key = Guid.Parse("{ABE78FA3-4B44-4E1F-97D0-F8B2BFC7BB52}"), RoleKey = Guid.Parse("{D000BC8D-4081-43FF-A4A4-AAC3B646D5ED}"), PowerKey = Guid.Parse("{0FFE3734-A059-4DD1-BA6F-74329858DB3D}") });

            // 外接应用系统信息
            context.Applications.Add(new Application { Key = Guid.Parse("{B3C18A96-AF53-414A-9E04-9AE7D869C172}"), Name = "测试系统01", Code = "B3C18A96-AF53-414A-9E04-9AE7D869C172" });
            context.Applications.Add(new Application { Key = Guid.Parse("{BBB34A59-2A2E-4806-89FD-CAE19F68EA6F}"), Name = "测试系统02", Code = "BBB34A59-2A2E-4806-89FD-CAE19F68EA6F" });
            context.Applications.Add(new Application { Key = Guid.Parse("{C8FBFADB-EADA-4D03-A402-873F7930AD7D}"), Name = "测试系统03", Code = "C8FBFADB-EADA-4D03-A402-873F7930AD7D" });
            context.Applications.Add(new Application { Key = Guid.Parse("{B29E6310-2AC8-4428-809C-42E04F4CF7E5}"), Name = "测试系统04", Code = "B29E6310-2AC8-4428-809C-42E04F4CF7E5" });
            context.Applications.Add(new Application { Key = Guid.Parse("{6CEE5DA0-A554-44CD-9F17-3F04D07A3026}"), Name = "测试系统05", Code = "6CEE5DA0-A554-44CD-9F17-3F04D07A3026" });
            context.Applications.Add(new Application { Key = Guid.Parse("{A5A1C7B2-8D8B-4F66-A972-5447881975BA}"), Name = "测试系统06", Code = "A5A1C7B2-8D8B-4F66-A972-5447881975BA" });
            context.Applications.Add(new Application { Key = Guid.Parse("{71851240-6A47-47FC-86BE-6F61BA9063FB}"), Name = "测试系统07", Code = "71851240-6A47-47FC-86BE-6F61BA9063FB" });
            context.Applications.Add(new Application { Key = Guid.Parse("{A77519F0-7CD6-4934-AFDE-DE990F3393F1}"), Name = "测试系统08", Code = "A77519F0-7CD6-4934-AFDE-DE990F3393F1" });
            context.Applications.Add(new Application { Key = Guid.Parse("{B04708BA-A962-41ED-B682-61EB661AB909}"), Name = "测试系统09", Code = "B04708BA-A962-41ED-B682-61EB661AB909" });
            context.Applications.Add(new Application { Key = Guid.Parse("{1B59F2B5-BC51-49D7-A713-21B9FE6FBE7A}"), Name = "测试系统10", Code = "1B59F2B5-BC51-49D7-A713-21B9FE6FBE7A" });
            context.Applications.Add(new Application { Key = Guid.Parse("{04D82387-FE10-43E3-942A-4DD4F6F63049}"), Name = "测试系统11", Code = "04D82387-FE10-43E3-942A-4DD4F6F63049" });
            context.Applications.Add(new Application { Key = Guid.Parse("{5F87FB09-2C0B-44BE-8181-EF8B6D6EEE2C}"), Name = "测试系统12", Code = "5F87FB09-2C0B-44BE-8181-EF8B6D6EEE2C" });
            context.Applications.Add(new Application { Key = Guid.Parse("{D56BECC7-AC48-4BD8-8EF6-E026708FE159}"), Name = "测试系统13", Code = "D56BECC7-AC48-4BD8-8EF6-E026708FE159" });
            context.Applications.Add(new Application { Key = Guid.Parse("{73BC70A9-12DB-4705-8727-DCEA83C6BBAE}"), Name = "测试系统14", Code = "73BC70A9-12DB-4705-8727-DCEA83C6BBAE" });
            context.Applications.Add(new Application { Key = Guid.Parse("{DACFE8C3-7F63-4AA6-A244-871ACC826152}"), Name = "测试系统15", Code = "DACFE8C3-7F63-4AA6-A244-871ACC826152" });

            // 外接应用系统访问权限

            // 外接应用系统的请求路径来源域集合
        }
    }
}
